<template>
    <PageView>
        <a-card title="列表">
            <a-row :gutter="10" class="inputGroup">
                <a-col :xs="12" :md="6" :lg="6" :xl="4">
                    <a-range-picker v-model="time" :placeholder="['开始时间', '结束时间']" @change="getListTwo"/>
                </a-col>
                <a-col :xs="12" :md="6" :lg="6" :xl="4">
                    <a-input v-model="searchData.username " placeholder="账号" @keyup.enter="getListTwo"/>
                </a-col>
                <a-col :xs="12" :md="6" :lg="6" :xl="4">
                    <a-select allowClear v-model="searchData.status" style="width:100%" placeholder="状态" @change="getListTwo">
                        <a-select-option value="0">匹配中</a-select-option>
                        <a-select-option value="1">已匹配</a-select-option>
                        <a-select-option value="2">已完成</a-select-option>
                    </a-select>
                </a-col>
                <a-col :xs="12" :md="6" :lg="6" :xl="4">
                    <a-select allowClear v-model="searchData.matc_status" style="width:100%" placeholder="是否禁止" @change="getListTwo">
                        <a-select-option value="1">未禁止</a-select-option>
                        <a-select-option value="-1">禁止</a-select-option>
                    </a-select>
                </a-col>
                <a-col :xs="12" :md="6" :lg="6" :xl="4">
                    <a-button-group>
                        <a-button type="primary" :loading="loading" @click="getListTwo">确定筛选</a-button>
                    </a-button-group>
                </a-col>
            </a-row>
            <a-table
                :loading="loading"
                :columns="columns"
                :dataSource="data"
                :pagination="{total,pageSize,showSizeChanger,current}"
                @change="changePagination"
                bordered :scroll="{x:true}">
            </a-table>
        </a-card>
    </PageView>
</template>

<script type="text/jsx">
    import Orders from '../../api/Orders'
    export default {
        name: 'outList',
        data () {
            return {
                loading: true,
                columns: [
                    {
                        title: '账号',
                        dataIndex: 'username',
                        customRender: (v) => <a>{v}</a>
                    },
                    {
                        title: '订单编号',
                        dataIndex: 'sn',
                    },
                    {
                        title: '出单时间',
                        dataIndex: 'add_time',
                        customRender: (v) => v?this.timeOne(v):"暂无"
                    },
                    {
                        title: '匹配金额',
                        dataIndex: 'coin',
                    },
                    {
                        title: '剩余匹配金额',
                        dataIndex: 'uncoin',
                    },
                    {
                        title: '已匹配金额',
                        dataIndex: 'picoin',
                    },
                    {
                        title: '交易方式',
                        dataIndex: 'payment',
                        customRender:(v)=> {
                            return {
                                1:"现金",
                                2:"USDT",
                                3:"现金+USDT"
                            }[v]||"暂无"
                        }
                    },
                    {
                        title: '排单方式',
                        dataIndex: 'type',
                        customRender: (v) => {
                            let status = {
                                1: { name: '普通', color: 'warning' },
                                2: { name: '急速', color: 'error' }
                            }
                            return <a-badge status={status[v].color} text={status[v].name}/>
                        }
                    },
                    {
                        title: '状态',
                        dataIndex: 'status',
                        customRender: (v) => {
                            let status = {
                                0: { name: '匹配中', color: 'error' },
                                1: { name: '已匹配', color: 'warning' },
                                2: { name: '已完成', color: 'success' },
                            }
                            return <a-badge status={status[v].color} text={status[v].name}/>
                        }
                    },
                    {
                        title: '是否禁止',
                        dataIndex: 'matc_status',
                        customRender: (v) => {
                            let status = {
                                1: { name: '未禁止', color: 'success' },
                                "-1": { name: '已禁止', color: 'error' }
                            }
                            return <a-badge status={status[v].color} text={status[v].name}/>
                        }
                    },
                    {
                        title: '操作',
                        key: 'action',
                        fixed: 'right',
                        width: 110,
                        customRender: (v) => {
                            return (
                                <a-dropdown>
                                    <a-menu slot="overlay">
                                        <a-menu-item onClick={() => {
                                            this.zj(v.id,v.matc_status)
                                        }}>{v.matc_status==="1"?"禁止":"撤销"}
                                        </a-menu-item>
                                        <a-menu-item disabled={v.matc_status==="1"} onClick={() => {
                                            this.cancle(v.id)
                                        }}>取消
                                        </a-menu-item>
                                        <a-menu-item disabled={v.status!=="0"} onClick={() => {
                                            this.$router.push({name:"pipei",query:{id:v.id}})
                                        }}>匹配
                                        </a-menu-item>
                                    </a-menu>
                                    <a-button>操作 <a-icon type="down"/></a-button>
                                </a-dropdown>
                            )
                        }
                    }
                ],
                data: [],
                total: 0,
                pageSize: 10,
                current: 1,
                showSizeChanger: true,
                searchData: {
                    username: '',
                    status:undefined,
                    matc_status:undefined
                },
                time: [],
            }
        },
        activated () {
            this.getList()
        },
        methods: {
            getListTwo () {
                this.current = 1
                this.getList()
            },
            getList () {
                this.loading = true
                Orders.buyOrderList({
                    page: this.current,
                    num: this.pageSize,
                    ...this.searchData,
                    begin: Date.parse(this.time[0]) / 1000 || undefined,
                    last: Date.parse(this.time[1]) / 1000 || undefined
                }).then(res => {
                    this.data = res.data.map((value, key) => {
                        return { ...value, key }
                    })
                    this.loading = false
                    this.total = parseInt(res.count)
                })
            },
            changePagination (pagination) {
                Object.keys(pagination).forEach(val => {
                    this[val] = pagination[val]
                })
                this.getList()
            },
            zj(id,type){
                this.$confirm({
                    title: `${type===`1`?`禁止`:`撤销`}`,
                    content: `确定要${type===`1`?`禁止`:`撤销`}此订单吗?`,
                    centered: true,
                    onOk: () => {
                        return new Promise((resolve, reject) => {
                            Orders.saveBuyMatcStatus({ id,type}).then(res => {
                                resolve()
                                this.getList()
                            }).catch(res => {
                                reject()
                            })
                        })
                    }
                })
            },
            cancle(id){
                this.$confirm({
                    title: `取消`,
                    content: `确定要取消此订单吗?`,
                    centered: true,
                    onOk: () => {
                        return new Promise((resolve, reject) => {
                            Orders.overMatcOrder({ id }).then(res => {
                                resolve()
                                this.getList()
                            }).catch(res => {
                                reject()
                            })
                        })
                    }
                })
            }
        }
    }
</script>

<style scoped lang="less">
    .inputGroup {
        > div {
            margin-bottom: 20px;
        }
    }
</style>